package JavaCode;

import java.util.Scanner;

/**
 * @Author Kechelle
 * @Date 2020/9/11 9:35
 * @Version 1.0
 */
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int lenth = scanner.nextInt();
        int[] num = new int[lenth];
        for (int i = 0; i < lenth; i++) {
            num[i] = scanner.nextInt();
        }
        System.out.println(function(num));
    }

    public static int function(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j]) {
                    sum = sum + Math.abs(nums[i] - nums[j]);
                }
            }
        }
        return sum;
    }
}

/**
 * 网易笔试第一道
 * public static String function(int x){
 *         StringBuffer sb = new StringBuffer();
 *         while (x-9>0){
 *             sb.append("9");
 *             x = x-9;
 *         }
 *         String res = String.valueOf(x) + sb.toString();
 *         return res;
 *     }
 */

/**
 * 网易笔试第三道
 * public static int function(int[] nums) {
 *         int sum = 0;
 *         for (int i = 0; i < nums.length; i++) {
 *             for (int j = i + 1; j < nums.length; j++) {
 *                 if (nums[i] > nums[j]) {
 *                     sum = sum + Math.abs(nums[i] - nums[j]);
 *                 }
 *             }
 *         }
 *         return sum;
 *     }
 */
